package com.ldg.seckill.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ldg.seckill.entity.TbSeckillGoods;
import com.ldg.seckill.mapper.TbSeckillGoodsMapper;
import com.ldg.seckill.service.TbSeckillGoodsService;
/**
 * @Author: Liudao
 * @Date: 2022-10-21-16:55
 * @Description:
 */
@Service
public class TbSeckillGoodsServiceImpl extends ServiceImpl<TbSeckillGoodsMapper, TbSeckillGoods> implements TbSeckillGoodsService{

    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private TbSeckillGoodsMapper goodsMapper;
    @Override
    public List<TbSeckillGoods> findSeckillGoodsByDate(String dateStr) {
        //根据key得到秒杀商品的集合
        BoundHashOperations<String, String, String> ops = redisTemplate.boundHashOps("seckillGoods_" + dateStr);
        List<String> values = ops.values();
        //处理这个字符串集合
        if (values!=null){
            return values.stream().map(m -> JSON.parseObject(m, TbSeckillGoods.class)).collect(Collectors.toList());
        }
        System.out.println("没有查询到可以开始秒杀的商品");
        return null;
    }

    @Override
    public List<TbSeckillGoods> findGoodsList(QueryWrapper<TbSeckillGoods> wrapper) {
        return goodsMapper.selectList(wrapper);
    }

    @Override
    public TbSeckillGoods findById(String secKillId) {
        return this.getById(secKillId);
    }
}
